package com.lib.service;


import com.lib.dao.TypeDao;
import com.lib.dao.ex.DaoException;
import com.lib.dao.ex.DeleteDaoException;
import com.lib.dao.ex.InsertDaoException;
import com.lib.dao.ex.UpdateDaoException;
import com.lib.entity.Type;
import com.lib.service.ex.DeleteServiceException;
import com.lib.service.ex.EmptyArgumentException;
import com.lib.service.ex.ServiceException;
import com.lib.service.ex.UpdateServiceException;

import java.util.List;

public class TypeService {
    //添加新类型

    private TypeDao typeDao = new TypeDao();

    public void addType(Type type) throws InsertDaoException, ServiceException, EmptyArgumentException {
        if (type == null) {
            throw new EmptyArgumentException("添加新类型异常 : type :null");
        }
        //TODO: type中的属性做非空判断
        try {
            typeDao.insertType(type);
        } catch (InsertDaoException e) {
            throw new InsertDaoException("添加新类型异常: 添加失败 :" + e.getMessage(), e);
        } catch (DaoException e) {
            throw new ServiceException("添加新类型失败:数据库异常" + e.getMessage(), e);
        }
    }

    //获取图书类型
    public List<Type> getTypeList() throws ServiceException {
        List<Type> typeList = null;

        try {
            typeList = typeDao.selectTypeListById();
        } catch (DaoException e) {
            throw new ServiceException("获取图书类型失败" + e.getMessage(), e);
        }
        return typeList;

    }

    //删除类型
    public void deleteType(int id) throws ServiceException, DeleteServiceException {

        try {
            typeDao.deleteTypeById(id);
        } catch (DaoException e) {
            throw new ServiceException("删除类型异常:数据库异常:" + e.getMessage(), e);
        } catch (DeleteDaoException e) {
            throw new DeleteServiceException("删除类型异常:删除失败" + e.getMessage(), e);
        }
    }

    //修改更新
    public void updateType(Type type) throws ServiceException, UpdateServiceException {
        try {
            typeDao.updateType(type);
        } catch (DaoException e) {
            throw new ServiceException("更新图书类型失败:数据库异常" + e.getMessage(), e);
        } catch (UpdateDaoException e) {
            throw new UpdateServiceException("更新图书类型失败" + e.getMessage(), e);
        }
    }


}
